<?php
session_start ();
require_once ("../commun/ouvre_base.php");
// ~ $link = mysqli_connect($host, $userbase, $passwd) or die('Impossible de se connecter pour récupérer niveau : ' . mysql_error());
mysql_select_db ( $db ) or die ( 'Impossible de s&#233;lectionner la base de donn&#233;es' );
// ~ mysqli_select_db($link,$db) or die('Impossible de s&#233;lectionner la base de donn&#233;es');

$_SESSION ['pathway'] = '..';
$_SESSION ['pathway_js'] = '/genetiquespataxeurospa';
include_once ($_SESSION ['pathway'] . "/header.php");

if (! isset ( $_SESSION ['id'] )) {
	echo "<table class='principale'><tr><td>Session expired</td></tr>";
	echo "<a href='" . $_SESSION ['pathway'] . "/index.php'>Start</a></table>";
} elseif ($_SESSION ['acces_genetique'] == 0) {
	echo "<table class='principale'><tr><td>Restricted area</td></tr>";
	echo "<a href='" . $_SESSION ['pathway'] . "/start.php'>Home</a></table>";
} else {
	
	$action = $_GET ['action']; // edit ou insert
	if ($action == 'edit') {
		echo "<img src='" . $_SESSION ['pathway'] . "/images/modifier.png' height='20' alt='modifier.png' /> ";
	} else if ($action == 'insert') {
		echo "<img src='" . $_SESSION ['pathway'] . "/images/add-icon.png' height='20' alt='button_add.png' /> ";
	}
	echo ucfirst ( $action ) . ' linkage' . " </p>";
	$table = 'linkage'; // edit ou insert
	
	$hash_champ_valeur = array ();
	if ($action == 'edit') { // on récupère les données dans la index déjà dans la base
		$index = $_GET ['index'];
		
		// ~ ------------------------ en cas de modifs sur les fichiers
		$query_fichiersattaches = "SELECT `fichiersattaches`
		FROM `linkage`
		WHERE `index` LIKE " . $_GET ['index'];
		// ~ echo $query_fichiersattaches;
		$resultat_fichiersattaches = mysql_query ( $query_fichiersattaches ) or die ( "impossible de remplir le questionnaire : " . mysql_error () );
		// ~ $resultat_fichiersattaches = mysqli_query($link,$query_fichiersattaches) or die("impossible de remplir le questionnaire : " . mysql_error());
		$fichiers_attaches_base = mysql_fetch_array ( $resultat_fichiersattaches );
		// ~ $fichiers_attaches_base = mysqli_fetch_array($resultat_fichiersattaches);
		
		$fichiersattaches = $fichiers_attaches_base ['fichiersattaches'];
		if (isset ( $_GET ['delete'] )) {
			$fichiersattaches = str_replace ( $_GET ['delete'] . ';', '', $fichiersattaches );
			unlink ( "../uploads/" . $_GET ['delete'] );
			$query_update_fichiers = "UPDATE `spatax`.`linkage` SET `fichiersattaches` = '$fichiersattaches' WHERE `linkage`.`index` = " . $_GET ['index'];
			mysql_query ( $query_update_fichiers ) or die ( "impossible d'updater fichiers : " . mysql_error () );
			// ~ mysqli_query($link,$query_update_fichiers) or die("impossible d'updater fichiers : " . mysql_error());
		}
		
		$requete_index = "SELECT * FROM `" . $table . "`
		WHERE `index` LIKE " . $_GET ['index'] . ";";
		// ~ echo $requete_index;
		$result_requete_index = mysql_query ( $requete_index ) or die ( "Impossible de recupérer les entetes : " . mysql_error () );
		// ~ $result_requete_index = mysqli_query($link,$requete_index) or die("Impossible de recupérer les entetes : " . mysql_error());
		
		while ( $ligne = mysql_fetch_assoc ( $result_requete_index ) ) {
			// ~ while($ligne = mysqli_fetch_assoc($result_requete_index)) {
			foreach ( $ligne as $champ => $valeur ) {
				$hash_champ_valeur [$champ] = $valeur;
			}
		}
		$code1 = $hash_champ_valeur ['code1'];
		$code2 = $hash_champ_valeur ['code2'];
		$code3 = $hash_champ_valeur ['code3'];
		$code4 = $hash_champ_valeur ['code4'];
		$alias = $hash_champ_valeur ['alias'];
		$gene = $hash_champ_valeur ['gene'];
		$status = $hash_champ_valeur ['statut'];
		$validation = $hash_champ_valeur ['validation'];
	} elseif ($action == 'insert') {
		require_once ("../requetes/recupere_max.php");
		
		if (isset ( $_SESSION ['code1'] )) {
			$code1 = $_SESSION ['code1'];
		}
		if (isset ( $_SESSION ['code2'] )) {
			$code2 = $_SESSION ['code2'];
		}
		if (isset ( $_SESSION ['code3'] )) {
			$code3 = $_SESSION ['code3'];
		}
		if (isset ( $_SESSION ['code4'] )) {
			$code4 = $_SESSION ['code4'];
		}
		if (isset ( $_SESSION ['status'] )) {
			$status = $_SESSION ['status'];
		}
		if (isset ( $_SESSION ['validation'] )) {
			$validation = $_SESSION ['validation'];
		}
	} else {
		echo "error";
	}
	
	if (isset ( $_FILES ['fichieraajouter'] ) and $_FILES ['fichieraajouter'] ['error'] == 0) {
		$tabledate = getdate ();
		$id_unique = $tabledate ['seconds'] . $tabledate ['minutes'] . $tabledate ['hours'] . $tabledate ['mday'] . $tabledate ['mon'] . $tabledate ['year'];
		move_uploaded_file ( $_FILES ['fichieraajouter'] ['tmp_name'], '../../uploads/' . $id_unique . '_' . basename ( $_FILES ['fichieraajouter'] ['name'] ) );
		echo $_FILES ['fichieraajouter'] ['tmp_name'] . " et " . '../../uploads/' . $id_unique . '_' . basename ( $_FILES ['fichieraajouter'] ['name'] );
		chmod ( '../../uploads/' . $id_unique . '_' . basename ( $_FILES ['fichieraajouter'] ['name'] ), 0777 );
		$hash_champ_valeur ['fichiersattaches'] .= $id_unique . '_' . basename ( $_FILES ['fichieraajouter'] ['name'] ) . ";";
	}
	
	// ~----------------------------------- linkage
	
	$disabled_tous = '';
	if ($_SESSION ['acces_genetique'] >= 2) {
		echo "<form action=\"./requetes/remplissage_linkage.php?action=$action&amp;index=$index&amp;table=linkage\" method='post' enctype='multipart/form-data'>";
	} else {
		$disabled_tous = 'disabled';
	}
	$disabled_droits_supremes = '';
	if ($_SESSION ['acces_genetique'] < 3) {
		$disabled_droits_supremes = 'disabled';
	}
	?>

<table class='principale'>
	<tr>
		<td><i>user : </i><input <?php echo $disabled_tous.' ';?> type="text"
			name="user" readonly="readonly" style="width: 200px; border: none;"
			value="<?php
	if (isset ( $hash_champ_valeur ['user'] ) and $hash_champ_valeur ['user'] != '') { // on considère que le champ est egal à '' uniquement lors de la création de la fiche qu'il n'est jamais vide après
		echo strtoupper ( $hash_champ_valeur ['user'] );
	} else {
		$hash_champ_valeur ['user'] = $_SESSION ['id'];
		echo strtoupper ( $hash_champ_valeur ['user'] );
	}
	?>" /></td>
		<td><i>Last modifier : </i><input <?php echo $disabled_tous;?>
			type="text" name="last_modifier" readonly="readonly"
			style="width: 200px; border: none;"
			value="<?php
	if (isset ( $hash_champ_valeur ['last_modifier'] ) and $hash_champ_valeur ['last_modifier'] != '') {
		echo strtoupper ( $hash_champ_valeur ['last_modifier'] );
	} else {
		echo strtoupper ( $hash_champ_valeur ['user'] );
	}
	?>" /></td>
	</tr>
</table>

<table class='principale'>
	<tr>
		<td>Code ID Family : <br /> <input <?php echo $disabled_tous.' ';?>
			type="text" maxlength="3" size="3" name="code1" id="code1"
			value='<?php if(isset($code1)){echo $code1;} ?>' />- <input
			<?php echo $disabled_tous.' ';?> type="text" maxlength="3" size="3"
			name="code2" id="code2"
			value='<?php if(isset($code2)){echo $code2;} ?>' />- <input
			<?php echo $disabled_tous.' ';?> type="text" maxlength="3" size="3"
			name="code3" id="code3"
			value='<?php if(isset($code3)){echo $code3;} ?>' />- <input
			<?php echo $disabled_tous.' ';?> type="text" maxlength="4" size="4"
			name="code4" id="code4"
			value='<?php if(isset($code4)){echo $code4;} ?>' /> &#42;
		</td>

		<td>and/or : External code : <br /> <input
			<?php echo $disabled_tous.' ';?> type='text' style='width: 100px;'
			name='codeexterne'
			value='<?php if(isset($hash_champ_valeur['codeexterne'])){echo $hash_champ_valeur['codeexterne'];} ?>' />&#42;
		</td>
		<td>Genethon code : <br /> <input <?php echo $disabled_tous.' ';?>
			type='text' style='width: 100px;' name='codegenethon'
			value='<?php if(isset($hash_champ_valeur['codegenethon'])){echo $hash_champ_valeur['codegenethon'];} ?>' /></td>
	</tr>
	<tr>
		<td>Center : <br /> <input <?php echo $disabled_tous.' ';?>
			type='text' style='width: 100px;' name='centre'
			value='<?php if(isset($hash_champ_valeur['centre'])){echo $hash_champ_valeur['centre'];} ?>' /></td>
		<td>Neurologist/Biologist : <br /> <input
			<?php echo $disabled_tous.' ';?> type='text' style='width: 100px;'
			name='neurologist'
			value="<?php if(isset($hash_champ_valeur['neurologist'])){echo $hash_champ_valeur['neurologist'];} else{ echo strtoupper($_SESSION['id']);} ?>" /></td>
	</tr>
</table>


<table class='principale'>
	<tr>
		<td>Date :</td>
		<td><input <?php echo $disabled_tous.' ';?> type='text'
			style='width: 100px;' name='date'
			value='<?php if(isset($hash_champ_valeur['date']) and $hash_champ_valeur['date'] != ''){ echo $hash_champ_valeur['date']; }else{ echo $_SESSION['date'];} ?>' &#42;(yyyy-mm-dd))/></td>
		<td></td>
		<td style="border: 1px solid #BCD0D6; padding: 10px;">Status : <select
			size='1' name='statut'>
				<option></option>
				<option value='ongoing'
					<?php if(isset($hash_champ_valeur['statut']) and $hash_champ_valeur['statut'] == 'ongoing'){ echo "selected='selected'"; } ?>>
					ongoing</option>
				<option value='done'
					<?php if(isset($hash_champ_valeur['statut']) and $hash_champ_valeur['statut'] == 'done'){ echo "selected='selected'"; } ?>>
					done</option>
				<option value='to do'
					<?php if(isset($hash_champ_valeur['statut']) and $hash_champ_valeur['statut'] == 'to do'){ echo "selected='selected'"; } ?>>
					to do</option>
		</select></td>
	</tr>
	<?php
	// ~ if($_SESSION['acces_genetique'] >= 3){
	// ~ echo "<tr><td>user : </td><td><input ".$disabled_tous." type='text' style='width:100px' name='user' value='";
	// ~ if(isset($hash_champ_valeur['user']) and $hash_champ_valeur['user'] != ''){
	// ~ echo $hash_champ_valeur['user']; } else{ echo strtoupper($_SESSION['id']);
	// ~ }
	// ~ echo "' /></td></tr>";
	// ~ }
	?>
	<tr>
		<td>Gene (ex: ATXN7)</td>
		<td><input <?php echo $disabled_tous.' ';?> type='text'
			style='width: 100px;' name='gene'
			value='<?php if(isset($gene)){echo $hash_champ_valeur['gene'];} ?>' /></td>
	</tr>
	<tr>
		<td>Alias (ex: SCA7)</td>
		<td><input <?php echo $disabled_tous.' ';?> type='text'
			style='width: 100px;' name='alias'
			value='<?php if(isset($alias)){echo $hash_champ_valeur['alias'];} ?>' /></td>
	</tr>


	<tr>
		<td>Mode :</td>
		<td><label><input <?php echo $disabled_tous.' ';?> type='radio'
				name='modelinkage' onChange="this.form.submit();" value='dominant'
				<?php if(isset($hash_champ_valeur['modelinkage']) and $hash_champ_valeur['modelinkage'] == 'dominant'){ echo "checked='checked'"; } ?> />
				dominant</label><br /> <label><input
				<?php echo $disabled_tous.' ';?> type='radio' name='modelinkage'
				onChange="this.form.submit();" value='recessif'
				<?php if(isset($hash_champ_valeur['modelinkage']) and $hash_champ_valeur['modelinkage'] == 'recessif'){ echo "checked='checked'"; } ?> />
				recessive</label><br /> <label><input
				<?php echo $disabled_tous.' ';?> type='radio' name='modelinkage'
				onChange="this.form.submit();" value='X-linked'
				<?php if(isset($hash_champ_valeur['modelinkage']) and $hash_champ_valeur['modelinkage'] == 'X-linked'){ echo "checked='checked'"; } ?> />
				X-linked</label><br /> <label><input
				<?php echo $disabled_tous.' ';?> type='radio' name='modelinkage'
				onChange="this.form.submit();" value='pseudodominant'
				<?php if(isset($hash_champ_valeur['modelinkage']) and $hash_champ_valeur['modelinkage'] == 'pseudodominant'){ echo "checked='checked'"; } ?> />
				pseudo-dominant</label><br /> <label><input
				<?php echo $disabled_tous.' ';?> type='radio' name='modelinkage'
				onChange="this.form.submit();" value='sporadique'
				<?php if(isset($hash_champ_valeur['modelinkage']) and $hash_champ_valeur['modelinkage'] == 'sporadique'){ echo "checked='checked'"; } ?> />
				sporadic</label></td>
	</tr>

	<tr>
		<td>Haplotype :</td>
		<td><?php
	
	$disabled_div1 = '';
	$disabled_div2 = '';
	if (isset ( $hash_champ_valeur ['modelinkage'] )) {
		if ($hash_champ_valeur ['modelinkage'] == 'recessif') {
			$display_div1 = "display:block;";
			$display_div2 = "display:none;";
			$disabled_div1 = '';
			$disabled_div2 = 'disabled';
		} else if ($hash_champ_valeur ['modelinkage'] == 'dominant') {
			$display_div2 = "display:block;";
			$display_div1 = "display:none;";
			$disabled_div2 = '';
			$disabled_div1 = 'disabled';
		} else if ($hash_champ_valeur ['modelinkage'] == 'X-linked' or $hash_champ_valeur ['modelinkage'] == 'pseudodominant') {
			$display_div2 = "display:block;";
			$display_div1 = "display:none;";
			$disabled_div1 = 'disabled';
			$disabled_div2 = '';
		} else if ($hash_champ_valeur ['modelinkage'] == 'sporadique') {
			$display_div1 = "display:none;";
			$display_div2 = "display:none;";
		}
	} else {
		$display_div1 = "display:none;";
		$display_div2 = "display:none;";
	}
	
	?>
	<select name='haplotype' size='1'>
				<option></option>
				<option <?php echo $disabled_div1; ?> value='not geno id'
					<?php if(isset($hash_champ_valeur['haplotype']) and $hash_champ_valeur['haplotype'] == 'not geno id'){ echo "selected='selected'"; } ?>>
					not geno id</option>
				<option <?php echo $disabled_div1; ?>
					value='geno id without homozygosy'
					<?php if(isset($hash_champ_valeur['haplotype']) and $hash_champ_valeur['haplotype'] == 'geno id without homozygosy'){ echo "selected='selected'"; } ?>>
					geno id without homozygosy</option>
				<option <?php echo $disabled_div1; ?> value='homozygosy'
					<?php if(isset($hash_champ_valeur['haplotype']) and $hash_champ_valeur['haplotype'] == 'homozygosy'){ echo "selected='selected'"; } ?>>
					homozygosy</option>
				<option></option>
				<option <?php echo $disabled_div2; ?> value='cosegregation'
					<?php if(isset($hash_champ_valeur['haplotype']) and $hash_champ_valeur['haplotype'] == 'cosegregation'){ echo "selected='selected'"; } ?>>
					cosegregation</option>
				<option <?php echo $disabled_div2; ?> value='no cosegregation'
					<?php if(isset($hash_champ_valeur['haplotype']) and $hash_champ_valeur['haplotype'] == 'no cosegregation'){ echo "selected='selected'"; } ?>>no
					cosegregation</option>
		</select></td>
	</tr>
	<tr>
		<td>Linkage :</td>
		<td><select name='linkage' size='1'
			onchange="toggleDisplay(this.options[this.selectedIndex].value, name)">
				<option></option>
				<option value='exclusion'
					<?php if(isset($hash_champ_valeur['linkage']) and $hash_champ_valeur['linkage'] == 'exclusion'){ echo "selected='selected'"; } ?>>
					exclusion</option>
				<option value='significant linkage'
					<?php if(isset($hash_champ_valeur['linkage']) and $hash_champ_valeur['linkage'] == 'significant linkage'){ echo "selected='selected'"; } ?>>significant
					linkage</option>
				<option value='other'
					<?php if(isset($hash_champ_valeur['linkage']) and $hash_champ_valeur['linkage'] == 'other'){ echo "selected='selected'"; } ?>>other</option>
		</select>
			<div style='display: none;' id='divlinkage'>
				<input <?php echo $disabled_tous.' ';?> type='text'
					style='width: 150px' name='linkage_other'
					value='<?php if(isset($hash_champ_valeur['linkage_other'])){ echo $hash_champ_valeur['linkage_other'];} ?>' />
			</div></td>
		<td>Lod score : <br /> <input <?php echo $disabled_tous.' ';?>
			type='text' style='width: 50px' name='lodscore'
			value='<?php if(isset($hash_champ_valeur['lodscore'])){ echo $hash_champ_valeur['lodscore'];} ?>' /></td>
	</tr>
	<tr>
		<td>Comments :</td>
		<td><textarea <?php echo $disabled_tous.' ';?>
				name='commentairelinkage' cols='20' rows='4'><?php if(isset($hash_champ_valeur['commentairelinkage'])){ echo $hash_champ_valeur['commentairelinkage'];} ?></textarea></td>
	</tr>
	<tr>
		<td>Attached file(s) :</td>
		<td>
	<?php
	
	if (isset ( $hash_champ_valeur ['fichiersattaches'] )) {
		$table_fichiers = explode ( ';', $hash_champ_valeur ['fichiersattaches'] );
		foreach ( $table_fichiers as $fichier_attache ) {
			if ($fichier_attache != '') {
				echo "<img src='" . $_SESSION ['pathway'] . "/images/poubelle.png' height='15' onclick=\"confirmer_suppression_fichier('" . $code1 . '-' . $code2 . '-' . $code3 . '-' . $code4 . "','$index','$table','$fichier_attache')\" /> <a target=_blank href='../uploads/$fichier_attache'>$fichier_attache</a><br />"; // on affiche juste la liste des fichiers déjà présents dans la base
			}
		}
	}
	
	?></td>

		<input <?php echo $disabled_tous.' ';?> type='hidden'
			id='fichiersattaches' name='fichiersattaches'
			value='<?php if(isset($hash_champ_valeur['fichiersattaches'])){ echo $hash_champ_valeur['fichiersattaches']; } ?>' />
		<td><input <?php echo $disabled_tous.' ';?> type='file'
			name='fichieraajouter' size="10" value='' /><input
			<?php echo $disabled_tous.' ';?> type='submit' name='submitprinc'
			value='upload' /><i> (max 10)</i></td>
	</tr>
</table>

<table class='principale'>
	<tr>
		<td><h3>Diagnosis :</h3></td>
		<td><label><input <?php echo $disabled_tous.' ';?> type='radio'
				name='diagnosticlinkage' value='linked'
				<?php if(isset($hash_champ_valeur['diagnosticlinkage']) and $hash_champ_valeur['diagnosticlinkage'] == 'linked'){ echo "checked='checked'"; } ?> />
				linked </label><br /> <label><input <?php echo $disabled_tous.' ';?>
				type='radio' name='diagnosticlinkage' value='excluded'
				<?php if(isset($hash_champ_valeur['diagnosticlinkage']) and $hash_champ_valeur['diagnosticlinkage'] == 'excluded'){ echo "checked='checked'"; } ?> />
				excluded </label><br /> <label><input
				<?php echo $disabled_tous.' ';?> type='radio'
				name='diagnosticlinkage' value='not informative'
				<?php if(isset($hash_champ_valeur['diagnosticlinkage']) and $hash_champ_valeur['diagnosticlinkage'] == 'not informative'){ echo "checked='checked'"; } ?> />
				not informative </label><br /> <label><input
				<?php echo $disabled_tous.' ';?> type='radio'
				name='diagnosticlinkage' value='putative linkage'
				<?php if(isset($hash_champ_valeur['diagnosticlinkage']) and $hash_champ_valeur['diagnosticlinkage'] == 'putative linkage'){ echo "checked='checked'"; } ?> />
				putative linkage </label></td>
	</tr>

	<tr>
		<td><h3>Validation :</h3></td>

		<td>
	<?php
	echo "<label><input " . $disabled_tous . ' ' . $disabled_droits_supremes . ' ' . "type='radio' name='validation' value='yes' ";
	if (isset ( $validation ) and $validation == 'yes') {
		echo "checked='checked'";
	}
	echo "/> yes </label><br />";
	echo "<label><input " . $disabled_tous . ' ' . $disabled_droits_supremes . ' ' . "type='radio' name='validation' value='no' ";
	if (isset ( $validation ) and $validation == 'no' or isset ( $validation ) and $validation == '') {
		echo "checked='checked'";
	}
	echo "/> no </label>";
	?></td>
	</tr>

	<tr>
		<td><h3>To do :</h3></td>
		<td><textarea <?php echo $disabled_tous.' ';?> name='afaire' cols='15'
				rows='3'><?php if(isset($hash_champ_valeur['afaire'])){echo $hash_champ_valeur['afaire'];} ?></textarea></td>
	</tr>
	<tr>
		<td><h3>Comments :</h3></td>
		<td><textarea <?php echo $disabled_tous.' ';?>
				name='commentairesconclusion' cols='15' rows='3'><?php if(isset($hash_champ_valeur['commentairesconclusion'])){ echo $hash_champ_valeur['commentairesconclusion'];} ?></textarea></td>
	</tr><?php
	
	if ($_SESSION ['acces_genetique'] >= 2) {
		echo "<tr><td><input " . $disabled_tous . " type='submit' name='submitprinc' value='Ok'/></td></tr>";
	}
	?>
	</table>

<?php
	if ($_SESSION ['acces_genetique'] >= 2) {
		echo "</form>";
	}
	?>
	
	
	<?php
}

include ($_SESSION ['pathway'] . "/footer.php");
?>
